package model;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Random;

public class Grille extends Observable{
	/**
	 * pos de 0 à size-1
	 */
	private Avatar avatar;
	/**
	 * Liste de passants
	 */
	private List<Passant> passants = new ArrayList<Passant>();
	/**
	 * grille de taille size x size
	 */
	private int size;
	

	public Grille(int size,int posAvatarX, int posAvatarY,int visionAvatar, int nbPassants, int visionPassant){
		this.size = size;
		avatar = new Avatar(new Point(posAvatarX,posAvatarY), visionAvatar);
		//avatar.setPos(new Point(posAvatarX,posAvatarY));//pour appeler notify et maj de display des passants		

		//placement aléatoire des passants
		Random randomGenerator = new Random();
		Point p;
		for (int i=0; i<nbPassants; i++){
			List<Point> listp = new ArrayList<Point>();
			int randomX, randomY;
			do{
				randomX = randomGenerator.nextInt(size-1);
				randomY = randomGenerator.nextInt(size-1);
				p = new Point(randomX, randomY);//générer random
			}
			while(listp.contains(p) || ((posAvatarX == randomX) && (posAvatarY == randomY))); //((posAvatarX == ((Double)(p.getX())).intValue()) && (posAvatarX == ((Double)(p.getY())).intValue())));
			listp.add(p);
			passants.add(new Passant(p, visionPassant, avatar));
		}
		avatar.notification();
	}

	public int nbPassants(){
		return passants.size();
	}
	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
		setChanged();
		notifyObservers();
	}
	public Avatar getAvatar() {
		return avatar;
	}

	public void setAvatar(Avatar avatar) {
		this.avatar = avatar;
		setChanged();
		notifyObservers();
	}
	/**
	 * Méthode appelée lq clic souris pour modifier pos avatar, notifie observateur de modele (vue) + observateur d'avatar (passant)
	 * @param pos position
	 */
	public void setPosAvatar(Point pos) {
		this.avatar.setPos(pos);
		setChanged();
		notifyObservers();
	}

	public List<Passant> getPassants() {
		return passants;
	}

	public void setPassants(List<Passant> passants) {
		this.passants = passants;
		setChanged();
		notifyObservers();
	}

	@Override
	public String toString() {
		return "Grille [avatar=" + avatar + ", passants=" + passants
				+ ", size=" + size + "]";
	}
	

}
